﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddSubDepartment : System.Web.UI.Page
{
    CommonMethods cMethod = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCompanies();
            BindBranches();
            BindDepartments();
            BindSubDepartment();
        }
    }
    private void SaveSubDepartment()
    {
        if (btnSaveSubDepartment.Text == "Save")
        {

            using (POSEntities db = new POSEntities())
            {
                SubDepartment Subdepartment = new SubDepartment();
                Subdepartment.SubDeptName = txtSubDepartmentName.Text;
                Subdepartment.Dept_ID = Convert.ToInt32(DDL_Department.SelectedValue);
                Subdepartment.Is_Deleted = false;
                db.SubDepartments.Add(Subdepartment);
                db.SaveChanges();
            }
        }
        else if (btnSaveSubDepartment.Text == "Update")
        {
            using (POSEntities db = new POSEntities())
            {
                Int32 id = Convert.ToInt32(btnSaveSubDepartment.CommandArgument);
                var Subdepartment = (from b in db.SubDepartments where (b.SubDeptID == id) select b).FirstOrDefault();
                Subdepartment.SubDeptName = txtSubDepartmentName.Text;
                Subdepartment.Dept_ID=Convert.ToInt32(DDL_Department.SelectedValue);
                db.SaveChanges();
            }
        }
        BindSubDepartment();
        clearForm();

    }
    private void BindDepartments()
    {
        if (DDL_Branch.Items.Count > 0)
        {
            DDL_Department.DataSource = cMethod.getDepartmentWithBranchId(Convert.ToInt32(DDL_Branch.SelectedValue));
            DDL_Department.DataBind();
        }
    }
    private void BindBranches()
    {
        if (DDL_Company.Items.Count > 0)
        {
            DDL_Branch.DataSource = cMethod.getBranchesWithCompanyId(Convert.ToInt32(DDL_Company.SelectedValue));
            DDL_Branch.DataBind();
        }
    }
    private void BindCompanies()
    {

        DDL_Company.DataSource = cMethod.getAllCompanies();
        DDL_Company.DataBind();

    }
    protected void DDL_Company_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBranches();
        BindDepartments();
    }

    protected void DDL_Branch_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartments();
    }

    protected void btnSaveSubDepartment_Click(object sender, EventArgs e)
    {
        SaveSubDepartment();
    }

    protected void lbtnEditSubDepartment_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var Subdepartment = (from sd in db.SubDepartments
                                 join d in db.Departments on sd.Dept_ID equals d.Dept_ID
                                 join b in db.Branches on d.BranchId equals b.BranchId
                              join c in db.Companies on b.CompanyId equals c.CompanyId
                              where (d.Dept_ID == cId) && d.Is_Deleted == false
                              select new { c.CompanyId, c.CompanyName, b.BranchId, b.BranchName, d.Dept_ID, d.Dept_Name,sd.SubDeptID,sd.SubDeptName }).FirstOrDefault();
            DDL_Company.SelectedValue = Subdepartment.CompanyId.ToString();
            BindBranches();
            DDL_Branch.SelectedValue = Subdepartment.BranchId.ToString();
            BindDepartments();
            DDL_Department.SelectedValue = Subdepartment.Dept_ID.ToString();
            txtSubDepartmentName.Text = Subdepartment.SubDeptName;
            btnSaveSubDepartment.CommandArgument = Subdepartment.SubDeptID.ToString();
            btnSaveSubDepartment.Text = "Update";
        }
    }
    private void clearForm()
    {
        txtSubDepartmentName.Text = "";
        btnSaveSubDepartment.Text = "Save";
        btnSaveSubDepartment.CommandArgument = "";
    }
    protected void lbtnDeleteSubDepartment_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        int cId = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db = new POSEntities())
        {
            var subdepartment = (from c in db.SubDepartments
                              where c.SubDeptID == cId
                              select c).FirstOrDefault();
            subdepartment.Is_Deleted = true;
            db.SaveChanges();
            BindDepartments();
        }
    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        clearForm();
    }

    protected void DDL_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubDepartment();
    }
    private void BindSubDepartment()
    {
        if (DDL_Department.Items.Count>0)
        {
            GridViewSubDepartments.DataSource =cMethod.getSubDepartmentWithDepartmentId(Convert.ToInt32(DDL_Department.SelectedValue));
            GridViewSubDepartments.DataBind();
        }
    }
}